package cn.doitedu.vueapi.controller.olap;

import cn.doitedu.vueapi.pojo.olap.Result;
import cn.doitedu.vueapi.pojo.olap.TableUser;
import cn.doitedu.vueapi.pojo.olap.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
public class TestLoginController {

    @CrossOrigin
    @PostMapping(value = "login2")
    @ResponseBody
    public Result login(@RequestBody User requestUser) {
        // 对 html 标签进行转义，防止 XSS 攻击
        String username = requestUser.getUsername();
        username = HtmlUtils.htmlEscape(username);

        if (!Objects.equals("admin", username) || !Objects.equals("123456", requestUser.getPassword())) {
            String msg = "账号密码错误";

            return new Result(400,null,msg);
        } else {
            User u = new User();
            u.setId(1);
            u.setName("多易教育");
            u.setUsername(requestUser.getUsername());
            u.setPassword(requestUser.getPassword());
            u.setAvatar("http://localhost:8080/static/user.png");
            return new Result(200,u,"");
        }
    }


    @CrossOrigin
    @GetMapping(value="user/list")
    @ResponseBody
    public List<TableUser>  getUser(){
        TableUser u1 = new TableUser(1, "zs", 0, 18, "2020-06-17", "hebei shijiazhuang");
        TableUser u2 = new TableUser(2, "ls", 1, 28, "1998-06-17", "hebei shijiazhuang");


        ArrayList<TableUser> users = new ArrayList<>();
        users.add(u1);
        users.add(u2);
        System.out.println("get user list");
        return users;
    }

    @CrossOrigin
    @GetMapping(value="user/listpage")
    @ResponseBody
    public List<TableUser> getUserPage(){

        TableUser u1 = new TableUser(1, "zs", 0, 18, "2020-06-17", "hebei shijiazhuang");
        TableUser u2 = new TableUser(2, "ls", 1, 28, "1998-06-17", "hebei shijiazhuang");


        ArrayList<TableUser> users = new ArrayList<>();
        users.add(u1);
        users.add(u2);
        System.out.println("get user list page");
        return users;
    }

}
